package com.by.yz.loginUtil;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.by.yz.App;

import java.net.CookieStore;
import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wlx on 6/14/16.
 */
public class LoginRESTFul {
    public static String api = "/api/authentication";
    public interface ILoginCb {
        public void ok(String response);

        public void error(String error);
    }

    public void login(final String passWord, final String phone, final ILoginCb cb) {

        String url = Constants.server_url + api;

        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

//                        if(response.contentEquals("phone")){
//                            if(response.contentEquals(passWord)){
//                                Log.e("sss","成功登陆");
//                            }else{
//                                Log.e("eee","密码错误");
//                            }
//                        }else{
//                            Log.e("eeee","账号不存在");
//                        }
                        cb.ok(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        cb.error(error.toString());
                    }
                }
        ) {


            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                // the POST parameters:
                params.put("j_password", passWord);
                params.put("j_username", phone);
                params.put("submit", "Login");
                params.put("remember-me", "true");

                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKEN")) {
                        Log.e("token-login", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }
                headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(postRequest);
    }
}
